import request from '@/utils/request';

const baseUrl = '/app-admin'
interface DictItem {
  itemDesc: string;
  itemValue: string;
}

const DICT_URL =  baseUrl + '/teacher-admin/admin/v1/sys/listItemByDictId';

const DICT_URL_V1 = baseUrl + '/teacher-admin/admin/common/listDictModel';

export const DICT_TYPE = {
  // 棋谱类型
  gameType : '1433595703550636036',
  // 复盘类型
   review : '1433595703550636035',

}

const dictCache = new Map();
// 根据groups选择用户
export async function selectStudent(params: any) {
  return await request(baseUrl + '/teacher-admin/admin/teacher/game/review/listFuzzyUserByCourse', {
    params
  });
}

export function getStudentSearchOptions(keys: any, data: any) {
  return !keys
    ? []
    : data.result.map((item: { uid: any; nickname: any; phone: any }) => ({
        id: item.uid,
        label: (item.nickname || '') + `  (${item.phone})`,
        value: item.nickname + `  (${item.phone})`,
      }));
}

export function getOptions(dictId: string) {
  return async () => {
    // 读缓存
    const item = dictCache.get(dictId);
    if (dictCache.get(dictId)) {
      return item;
    }

    const res = await queryDict(dictId);

    if (res && res.success) {
      const list = res.result.map((item: DictItem) => ({
        label: item.itemDesc,
        value: item.itemValue,
      }));

      dictCache.set(dictId, list);
      return list;
    }
  };
}

export function getOptionsV1(dictId: string) {
  return async () => {
    // 读缓存
    const item = dictCache.get(dictId);
    if (dictCache.get(dictId)) {
      return item;
    }

    const res = await queryDictV1(dictId);

    if (res && res.success) {
      const list = res.result.map((item: any) => ({
        label: item.text,
        value: item.value,
      }));
      // list.push({
      //   label: '-',
      //   value: '-1',
      // });
      dictCache.set(dictId, list);
      return list;
    }
  };
}

// 请求字典
export async function queryDict(dictId: string) {
  return request(DICT_URL, {
    params: {
      dictId,
    },
  });
}

export async function queryDictV1(type: string) {
  return request(DICT_URL_V1, {
    params: {
      type,
    },
  });
}
